package com.zz.nondatain.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.zz.nondatain.R;
import com.zz.nondatain.bean.Announcement;
import com.zz.nondatain.utils.DateUtils;

/**
 * 公告详情页面
 */
public class AnnouncementDetailActivity extends AppCompatActivity {

    private static final String EXTRA_ANNOUNCEMENT = "announcement";

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvPublisher;
    private TextView tvTime;

    private Announcement announcement;

    /**
     * 启动公告详情页面
     *
     * @param context      上下文
     * @param announcement 公告对象
     */
    public static void start(Context context, Announcement announcement) {
        Intent intent = new Intent(context, AnnouncementDetailActivity.class);
        intent.putExtra(EXTRA_ANNOUNCEMENT, announcement);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_detail);

        // 设置返回按钮
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("公告详情");
        }

        initViews();
        initData();
    }

    private void initViews() {
        tvTitle = findViewById(R.id.tv_announcement_title);
        tvContent = findViewById(R.id.tv_announcement_content);
        tvPublisher = findViewById(R.id.tv_announcement_publisher);
        tvTime = findViewById(R.id.tv_announcement_time);
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            announcement = (Announcement) intent.getSerializableExtra(EXTRA_ANNOUNCEMENT);
            if (announcement != null) {
                displayAnnouncementDetail(announcement);
                
                // 设置已读状态
                if (!announcement.isRead()) {
                    announcement.setRead(true);
                    // 如果有需要，可以在这里更新数据库中的已读状态
                }
            }
        }
    }

    /**
     * 显示公告详情
     *
     * @param announcement 公告对象
     */
    private void displayAnnouncementDetail(Announcement announcement) {
        // 设置标题
        if (!TextUtils.isEmpty(announcement.getTitle())) {
            tvTitle.setText(announcement.getTitle());
        }

        // 设置内容
        if (!TextUtils.isEmpty(announcement.getContent())) {
            tvContent.setText(announcement.getContent());
        }

        // 设置发布者
        if (!TextUtils.isEmpty(announcement.getPublisher())) {
            tvPublisher.setText(String.format("发布者：%s", announcement.getPublisher()));
        } else {
            tvPublisher.setText("发布者：系统");
        }

        // 设置发布时间
        if (announcement.getPublishTime() > 0) {
            tvTime.setText(String.format("发布时间：%s", DateUtils.formatFullDateTime(announcement.getPublishTime())));
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 